import dtlCtrl from './detail/detail.ctrl.js';
import dtlView from './detail/detail.html';


class Alarm {
    constructor(clusters, list, alarmSrv, $uiModal, ctgNotification, $scope) {
        "ngInject";
        this.alarmSrv = alarmSrv;
        this.uiModal = $uiModal;
        this._notification = ctgNotification;
        this.clusters = clusters && clusters.list;

        this.typeList =  [{name:'未处理',val:true},{name:'已处理',val:false}];
        // 筛选属性
        this.filter = {
            alertType: null,
            isactive: true,
            startTime: null,
            endTime: null,
            clusterInst: clusters && clusters.list.length && clusters.list[0]
        };
        this.page = {
            perPage: 10,
            currentPage: 1,
            totalItems: 0
        };
        if (list) {
            this.table = list.alertInfos;
            this.page.totalItems = list.alertNum;
        } else this.table = [];

        this.datepicker = {max: new Date()};
        $scope.$watch('list.filter.startTime', function (newVal, oldVal) {
            if (newVal !== oldVal) {
                $scope.$broadcast('pickerUpdate', 'endTime', {minDate: newVal});
            }
        });
        $scope.$watch('list.filter.endTime', function (newVal, oldVal) {
            if (newVal !== oldVal) {
                $scope.$broadcast('pickerUpdate', 'startTime', {maxDate: newVal});
            }
        });
    }


    // 获取告警信息列表
    getList() {
        if(this.filter.clusterInst){
            this.table = null;
            this.alarmSrv.getAlarmListOfK8s(this.page, this.filter).then(data => {
                this.table = data.alertInfos;
                this.page.totalItems = data.alertNum;
            }).catch(()=> {
                this.table = [];
                this.page.totalItems = 0;
                this._notification.error('获取容器集群告警列表数据有误.');
            });
        }
    }

    search() {
        this.page.currentPage = 1;
        this.getList();
    }

    openDetailForm(alarm) {
        this.uiModal.open({
            animation: true,
            templateUrl: dtlView,
            controller: dtlCtrl,
            controllerAs: 'dtl',
            resolve: {
                alarm: ()=> {
                    alarm.clusterName = this.filter.clusterInst.clusterName;
                    return alarm;
                }
            }
        });
    }
}
export default Alarm